package com.syni.mdd.sjb.common.datatreaure.quartz;

import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankCdpOrderTotal;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankHomePageStatistics;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.CdpUtilsBo;
import com.syni.mdd.sjb.common.datatreaure.service.DankCdpOrderTotalService;
import com.syni.mdd.sjb.common.datatreaure.service.DankHomePageStatisticsService;
import com.syni.mdd.sjb.common.datatreaure.service.UserService;
import com.syni.mdd.sjb.common.datatreaure.utils.CDPUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Component
@EnableScheduling
public class CdpQuartz {

    @Autowired
    DankHomePageStatisticsService dankHomePageStatisticsService;

    @Autowired
    UserService userService;

    @Autowired
    DankCdpOrderTotalService dankCdpOrderTotalService;

    /**
     * 处理待处理订单数
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void refreshBusinessOrdersProcessed(){
        Date nowTime = new Date();
        Date endTime = DateTimeUtils.getStartTimeOfDay(nowTime);
        Date startTime = DateTimeUtils.addDay(endTime,-1);
        String endTimeStr = DateTimeUtils.converDateToString(endTime,DateTimeUtils.DATE_PATTERN_DAY);
        String startTimeStr = DateTimeUtils.converDateToString(startTime,DateTimeUtils.DATE_PATTERN_DAY);
        List<DankHomePageStatistics> dankHomePageStatisticsList = dankHomePageStatisticsService.list();
        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        List<DankCdpOrderTotal> dankCdpOrderTotals = new LinkedList<>();
        if(CollectionUtils.isNotEmpty(dankHomePageStatisticsList)){
            for(DankHomePageStatistics dankHomePageStatistics : dankHomePageStatisticsList){
                CDPUtils.orderOrderTotal(cdpUtilsBo,userService.getAccountIdByUserMap(dankHomePageStatistics.getUserId()),startTimeStr,startTimeStr);
                DankCdpOrderTotal dankCdpOrderTotal = new DankCdpOrderTotal();
                dankCdpOrderTotal.setUserId(dankHomePageStatistics.getUserId());
                dankCdpOrderTotal.setStatTime(DateTimeUtils.converDateToInterger(startTime));
                dankCdpOrderTotal.setTotalAmount(BigDecimal.valueOf(cdpUtilsBo.getTotalAmount()));
                dankCdpOrderTotal.setOrderQuantity(new BigDecimal(cdpUtilsBo.getOrderQuantity().doubleValue()));
                dankCdpOrderTotals.add(dankCdpOrderTotal);
            }
        }
        if(dankCdpOrderTotals.size() > 0){
            dankCdpOrderTotalService.saveBatch(dankCdpOrderTotals);
        }

    }

    /**
     * 处理客单价
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void orderPerCustomerTransaction(){
        Date nowTime = new Date();
        Date endTime = DateTimeUtils.getStartTimeOfDay(nowTime);
        Date startTime = DateTimeUtils.addDay(endTime,-1);
        String endTimeStr = DateTimeUtils.converDateToString(endTime,DateTimeUtils.DATE_PATTERN_DAY);
        String startTimeStr = DateTimeUtils.converDateToString(startTime,DateTimeUtils.DATE_PATTERN_DAY);
        List<DankHomePageStatistics> dankHomePageStatisticsList = dankHomePageStatisticsService.list();
        CdpUtilsBo cdpUtilsBo = new CdpUtilsBo();
        List<DankCdpOrderTotal> dankCdpOrderTotals = new LinkedList<>();
        if(CollectionUtils.isNotEmpty(dankHomePageStatisticsList)){
            for(DankHomePageStatistics dankHomePageStatistics : dankHomePageStatisticsList){
                CDPUtils.orderOrderTotal(cdpUtilsBo,userService.getAccountIdByUserMap(dankHomePageStatistics.getUserId()),startTimeStr,startTimeStr);
                DankCdpOrderTotal dankCdpOrderTotal = new DankCdpOrderTotal();
                dankCdpOrderTotal.setUserId(dankHomePageStatistics.getUserId());
                dankCdpOrderTotal.setStatTime(DateTimeUtils.converDateToInterger(startTime));
                dankCdpOrderTotal.setTotalAmount(BigDecimal.valueOf(cdpUtilsBo.getTotalAmount()));
                dankCdpOrderTotal.setOrderQuantity(new BigDecimal(cdpUtilsBo.getOrderQuantity().doubleValue()));
                dankCdpOrderTotals.add(dankCdpOrderTotal);
            }
        }
        if(dankCdpOrderTotals.size() > 0){
            dankCdpOrderTotalService.saveBatch(dankCdpOrderTotals);
        }

    }
}
